<template>
  <div>
    <div>
      <FormPanel ref="formPanel_baseInfo" :list="formList_baseInfo" :cols="3" :labelWidth="100" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle_baseInfo" />
      <FilterTable
        ref="table"
        height="auto"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'rows'"
        :isSelectColumn="altBtnState"
        columnsRef="lxjczclglvehicleInsuranceTable"
        :onColumnsChange="columns => (this.columns = columns)"
      />
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import * as validate from '@/utils/validate.js';
import { isAuthorized } from '@/utils/index';
import { getInsuranceInfo, getVehicleInfoByVinAndDealerCode } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import { testNumLetter } from '../../../../../utils/validate';
export default {
  name: 'addVehicle_insurance',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    resData: {
      type: Object,
      default: {}
    }
  },
  data() {
    this.allData = {};
    this.saveData_baseInfo = {};
    return {
      newDate: '',
      formList_baseInfo: this.createFormList_baseInfo(),
      all_flag: false,
      baseInfo_flag: false,
      fetchApi: getInsuranceInfo,
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns(),
      altBtnState: false
    };
  },
  computed: {},
  watch: {
    newDate() {
      this.invoiceDateChange();
    }
  },
  created() {
    // this.getResData();
  },
  mounted() {
    // this.getSystemAuthority();
    console.log('保险起修', this.resData);
  },
  methods: {
    createFormList_baseInfo() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '保修信息'
        },
        {
          type: 'INPUT',
          label: '日均行驶里程',
          fieldName: 'dailyAverageMileage',
          disabled: true,
          initialValue: this.resData.dailyAverageMileage
        },
        {
          type: 'DATE',
          label: '保修起始日期',
          fieldName: 'wrtBeginDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true,
          initialValue: this.resData.wrtBeginDate
        },
        {
          type: 'DATE',
          label: '保修结束日期',
          fieldName: 'wrtEndinDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true,
          initialValue: this.resData.wrtEndinDate
        },
        {
          type: 'DATE',
          label: '首次进厂日期',
          fieldName: 'firstInDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true,
          initialValue: this.resData.firstInDate
        },
        {
          type: 'INPUT_NUMBER',
          label: '保修起始里程',
          fieldName: 'wrtBeginNileage',
          precision: 2,
          initialValue: this.resData.wrtBeginNileage,
          disabled: true,
          rules: [{ validator: validate.testNumLetter, trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '上次维修日期',
          fieldName: 'lastMaintainDate',
          disabled: true,
          valueFormat: 'yyyy-MM-dd',
          initialValue: this.resData.lastMaintainDate
        },
        {
          type: 'DATE',
          label: '上次保养日期',
          fieldName: 'lastMaintenanceDate',
          disabled: true,
          valueFormat: 'yyyy-MM-dd',
          initialValue: this.resData.lastMaintenanceDate
        },
        {
          type: 'DATE',
          label: '下次保养日期',
          fieldName: 'nextMaintainDate',
          valueFormat: 'yyyy-MM-dd',
          initialValue: this.resData.nextMaintainDate
        },
        {
          type: 'INPUT_NUMBER',
          label: '保修结束里程',
          fieldName: 'wrtEndNileage',
          precision: 2,
          initialValue: this.resData.wrtEndNileage,
          disabled: true,
          rules: [{ validator: validate.testNumLetter, trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '上次维修里程',
          fieldName: 'lastMaintainMileage',
          disabled: true,
          initialValue: this.resData.lastMaintainMileage
        },
        {
          type: 'INPUT',
          label: '上次保养里程',
          fieldName: 'lastMaintenanceMileage',
          disabled: true,
          initialValue: this.resData.lastMaintenanceMileage
        },
        {
          type: 'INPUT',
          label: '下次保养里程',
          fieldName: 'nextMaintainMileage',
          initialValue: this.resData.nextMaintainMileage,
          rules: [{ validator: validate.testNumLetter, trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '保险到期日期',
          fieldName: 'expiredDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true,
          initialValue: this.resData.expiredDate
        },
        {
          type: 'DATE',
          label: '本店保险最近购买日期',
          fieldName: 'instruaBuyDate',
          valueFormat: 'yyyy-MM-dd',
          labelWidth: '150px',
          disabled: true,
          initialValue: this.resData.instruaBuyDate
          // selfCols: 2
        },
        {
          type: 'INPUT',
          label: '延保信息',
          fieldName: 'conditiongDescription',
          disabled: true,
          initialValue: this.resData.conditiongDescription
        },
        {
          type: 'SELECT',
          label: '是否激活',
          fieldName: 'isActivaton',
          itemList: this.createDictList('1004'),
          initialValue: this.resData.isActivaton,
          change: val => {
            return this.isValidSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '不激活原因',
          fieldName: 'noValidReason',
          itemList: this.createDictList('8076'),
          initialValue: this.resData.noValidReason
        },
        {
          type: 'INPUT',
          label: '注意事项',
          fieldName: 'attentionInfo',
          disabled: true,
          initialValue: this.resData.attentionInfo
        }
      ];
    },
    invoiceDateChange() {
      console.log(this.newDate, 'newDate insurance');

      let date = new Date(this.newDate);
      date.setFullYear(date.getFullYear() + 3);
      date.setDate(date.getDate() - 1);
      this.formList_baseInfo.find(item => item.fieldName === 'wrtBeginDate').initialValue = this.newDate;
      this.formList_baseInfo.find(item => item.fieldName === 'wrtEndinDate').initialValue = date;
      this.formList_baseInfo.find(item => item.fieldName === 'wrtBeginNileage').initialValue = 0;
      this.formList_baseInfo.find(item => item.fieldName === 'wrtEndNileage').initialValue = 100000;
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '险种',
          dataIndex: 'INSURANCE_TYPE_NAME',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '保险公司',
          dataIndex: 'INSURATION_SHORT_NAME',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '保险开始时间',
          dataIndex: 'INSURANCE_BEGIN_DATE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '保险结束时间',
          dataIndex: 'INSURANCE_END_DATE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '保单号',
          dataIndex: 'INSURANCE_BILL_NO',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否续保',
          dataIndex: 'RE_INSURE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <div>{this.createDictText(props.row.RE_INSURE, '1004')}</div>;
          }
        },
        {
          title: '实际费用',
          dataIndex: 'ACTUAL_EXPENDING',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '续保专员',
          dataIndex: 'COMPLETED_BY',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '登记日期',
          dataIndex: 'RECORD_DATE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '登记人',
          dataIndex: 'RECORDER',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否续保提醒',
          dataIndex: 'IS_INSURE_REMIND',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004')
        },
        {
          title: '投保日期',
          dataIndex: 'INSURANCE_BUY_DATE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '完成日期',
          dataIndex: 'COMPLETED_DATE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '是否本公司投保',
          dataIndex: 'IS_SELF_COMPANY_INSURANCE',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <div>{this.createDictText(props.row.IS_SELF_COMPANY_INSURANCE, '1004')}</div>;
          }
        },
        {
          title: '是否销售订单创建',
          dataIndex: 'IS_SALE_CREATE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004')
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    formChangeHandle_baseInfo(val) {
      this.saveData_baseInfo = val;
      this.baseInfo_flag = true;
      this.allData = Object.assign(this.allData, this.saveData_baseInfo);
    },
    getFlag() {
      // if (this.baseInfo_flag) {
      //   this.all_flag = true;
      // } else {
      //   this.all_flag = false;
      // }
      this.all_flag = true;

      return this.all_flag;
    },
    getData() {
      console.log(this.baseInfo_flag, 'this.baseInfo_flag');
      return this.allData;
    },
    isValidSelect(val) {
      if (val == 10041001) {
        this.formList_baseInfo.find(item => item.fieldName === 'noValidReason').disabled = true;
        this.formList_baseInfo.find(item => item.fieldName === 'noValidReason').initialValue = undefined;
      } else {
        this.formList_baseInfo.find(item => item.fieldName === 'noValidReason').disabled = false;
      }
    },

    async getResData() {
      if (this.type != 'add') {
        let parm = {
          vin: this.resData.VIN,
          ownerCode: this.resData.OWNER_CODE
        };
        const res = await getVehicleInfoByVinAndDealerCode(parm);
        this.formList_baseInfo.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        let wrtDate = [];
        let wrtNileage = [];
        wrtDate.push(res.data.wrtBeginDate);
        wrtDate.push(res.data.wrtEndinDate);
        wrtNileage.push(res.data.wrtBeginNileage);
        wrtNileage.push(res.data.WRT_END_MILEAGE);
        this.formList_baseInfo.find(item => item.fieldName === 'LastMaintainMileage').initialValue = res.data.lastMaintainMileage;
        this.formList_baseInfo.find(item => item.fieldName === 'wrtBeginDate|wrtEndinDate').initialValue = wrtDate;
        this.formList_baseInfo.find(item => item.fieldName === 'wrtBeginNileage|wrtEndNileage').initialValue = wrtNileage;
        this.formList_baseInfo = [...this.formList_baseInfo];
        // 加载车辆的保险信息
        this.fetchParams = Object.assign({}, parm, { xhrAbort: false });
        // this.a.push(this.fetchParams);
      }
    },
    // 获取系统权限
    getSystemAuthority() {
      // 保修信息
      console.log('11111------------');
      if (this.type === 'edit') {
        let flagResult = isAuthorized('1211', '12111029');
        console.log(flagResult, 'flagResult');
        if (flagResult) {
          console.log('12111029');
          this.formList_baseInfo.forEach(x => {
            if (
              x.fieldName === 'dailyAverageMileage' ||
              x.fieldName === 'firstInDate' ||
              x.fieldName === 'lastMaintainDate' ||
              x.fieldName === 'LastMaintainMileage' ||
              x.fieldName === 'lastMaintenanceDate' ||
              x.fieldName === 'lastMaintenanceMileage'
            ) {
              x.disabled = true;
            } else {
              x.disabled = false;
            }
          });
          this.formList_baseInfo = [...this.formList_baseInfo];
          this.altBtnState = true;
        } else {
          console.log('1211102922222--------------');
          // this.$notify.warning({ title: '提示信息', message: '您没有权限修改保险保修!' });
          this.formList_baseInfo.forEach(x => (x.disabled = true));
          this.formList_baseInfo = [...this.formList_baseInfo];
          this.altBtnState = false;
        }
      }
    },
    async getData() {
      let [err, data] = await this.$refs.formPanel_baseInfo.GET_FORM_DATA();
      return data;
    }
  }
};
</script>

<style lang="less" scoped></style>
